## The type of library we want to build. Possible values:
##   relocatable
##   static
LIBRARY_TYPE=static

# You may edit this makefile as long as you keep these original 
# target names defined.
MODE=distrib
GNATMAKE=@GNATMAKE@
GNATCLEAN=gnatclean
SVN=svn
INSTALL=@INSTALL@

BUILDS_SHARED=@BUILDS_SHARED@

version=@EL_VERSION@

distdir=ada-el-@EL_VERSION@

DIST_FILE=ada-el-@EL_VERSION@.tar.gz

LN_S=@LN_S@
MKDIR=mkdir
CP=cp
LN=ln -s

ifeq (${OS},Windows_NT)
LIBEXT=dll
LIBVER=dll
else
LIBEXT=so
LIBVER=so.$(version)
endif

srcdir = .
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
top_builddir = .

includedir=${prefix}/share/ada/adainclude
projectdir=${prefix}/share/ada/adainclude
bindir=${prefix}/bin
libdir=${prefix}/lib
alidir=${libdir}/ada/adalib/
libname=libada_el
docdir=libada-el
samplesdir=${prefix}/share/doc/$(docdir)

CPUS := @NR_CPUS@
MAKE_ARGS=-XMODE=${MODE} -XLIBRARY_TYPE=${LIBRARY_TYPE} -XPROCESSORS=$(CPUS)

GPRPATH=el.gpr

ifeq (${BUILDS_SHARED},yes)
all:     static shared
install: install_static install_shared
else
all:     static
install: install_static
endif

# Build executables for all mains defined by the project.
build:
	$(GNATMAKE) -m -p -P"$(GPRPATH)" $(MAKE_ARGS)

static:
	$(MAKE) LIBRARY_TYPE=static build

shared:
	$(MAKE) LIBRARY_TYPE=relocatable build

# Not intended for manual invocation.
# Invoked if automatic builds are enabled.
# Analyzes only on those sources that have changed.
# Does not build executables.
autobuild:
	$(GNATMAKE) -gnatc -c -k  -P "$(GPRPATH)"

# Clean the root project of all build products.
clean:
	-$(GNATCLEAN) -q -P "$(GPRPATH)" -XLIBRARY_TYPE=static
	-$(GNATCLEAN) -q -P "$(GPRPATH)" -XLIBRARY_TYPE=relocatable
	-$(GNATCLEAN) -q -Ptests
	-rm -f lib/*.a lib/*.$(DLLEXT)

# Clean root project and all imported projects too.
clean_tree:
	$(GNATCLEAN) -q -P "$(GPRPATH)" -r

# Check *all* sources for errors, even those not changed.
# Does not build executables.
analyze:
	$(GNATMAKE) -f  -gnatc -c -k  -P "$(GPRPATH)"

# Clean, then build executables for all mains defined by the project.
rebuild: clean build

# Build and run the unit tests
test:	build
	$(GNATMAKE) -p -Ptests
	bin/el_harness -xml el-aunit.xml

# Files to package in source distribution
DISTFILES=README LICENSE.txt NOTICE.txt NEWS CHANGES ChangeLog config.guess configure config.sub \
configure.in Makefile.in install-sh \
el.gpr.in tests.gpr.in samples.gpr config.gpr \
src samples regtests \
doc distrib debian

dist:
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)
	here=`cd $(top_builddir) && pwd`; \
	top_distdir=`cd $(distdir) && pwd`; \
	distdir=`cd $(distdir) && pwd`; \
	for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    cp -pr $$d/$$file $(distdir)/$$file; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done
	rm -f $(distdir)/distrib/*.gpr
	rm -rf $(distdir)/regtests/result
	mkdir $(distdir)/bin
	mkdir $(distdir)/obj
	mkdir $(distdir)/lib
	tar --exclude=.svn --exclude="*~" --exclude="*.o" --exclude="*.ali" \
	    --exclude="*.log" --exclude="*.a" \
	    -cvzf $(DIST_FILE) $(distdir)

install_static:
	$(MAKE) LIBRARY_TYPE=static install_lib

install_shared:
	$(MAKE) LIBRARY_TYPE=relocatable install_lib

install_lib:
	$(MKDIR) -p ${includedir}/ada-el
	$(MKDIR) -p ${projectdir}/
	${MKDIR} -p ${alidir}/ada-el ${bindir}
	${CP} -p src/*.ad[bs] ${includedir}/ada-el
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/el.gpr > ${projectdir}/el.gpr
	${CP} -p lib/$(LIBRARY_TYPE)/*.ali ${alidir}/ada-el/
ifeq ($(LIBRARY_TYPE),static)
	${CP} lib/$(LIBRARY_TYPE)/$(libname).a ${libdir}/
else
	${CP} -p lib/$(LIBRARY_TYPE)/$(libname).$(LIBVER) ${libdir}/
ifneq (${OS},Windows_NT)
	cd ${libdir} && \
	   rm -f $(libname).$(LIBEXT) && \
	   ${LN} $(libname).$(LIBVER) $(libname).$(LIBEXT)
else
	${CP} -p lib/$(LIBRARY_TYPE)/$(libname).$(LIBVER) ${bindir}/
endif
endif

install_samples:
	$(MKDIR) -p $(samplesdir)/samples
	cp -rp $(srcdir)/samples/*.ad[sb] $(samplesdir)/samples/
	cp -p $(srcdir)/samples.gpr $(samplesdir)
	cp -p $(srcdir)/config.gpr $(samplesdir)

uninstall:
	rm -rf ${includedir}/ada-el
	rm -rf ${alidir}/ada-el
	rm -f ${libdir}/$(libname).a
	rm -f ${libdir}/$(libname)l.$(LIBEXT)
	rm -f ${projectdir}/el.gpr

# prepare before releasing a new version of the package
# (collect information about changes and SVN repository)
prepare-release:
	${SVN} update
	${SVN} info > CHANGES
	${SVN} log -v > ChangeLog
